package com.tpix.photo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.googlecode.objectify.*;
import com.googlecode.objectify.util.DAOBase;

public class PhotoDAO extends DAOBase  {
	
	//register the class with objectify
	static{
		ObjectifyService.register(Photo.class);
	}
	
	public Photo save(Photo photo){
		ofy().put(photo);
		return photo;
	}
	
	public Photo findById(Long id){
		Key<Photo> key = new Key<Photo>(Photo.class,id);
		return ofy().get(key);
	}
	
	public List<String> getThreeURLs(){
		List<String> urls = new ArrayList<String>();
		ImagesService imageService = ImagesServiceFactory.getImagesService();
		Objectify ofy = ObjectifyService.begin();
		Iterable<Key<Photo>> photoKeys = ofy.query(Photo.class).fetchKeys();
		//now we have all the photo keys so get three urls
		Iterator<Key<Photo>> itr = photoKeys.iterator();
		for(int i=0;i<3;i++){
			if(itr.hasNext()){
				//get a url
				BlobKey bk = new BlobKey(ofy.get(itr.next()).getBlobKey());
				/** 
				 * Testing code: swap 
				 *    'TestImageService tis = new TestImageService();'
				 *    'String temp = tis.getString();'
				 * 
				 * with 
				 * 
				 *    'String temp = imageService.getServingUrl(bk);' 
				 * to test
				 */
				 //TestImageService tis = new TestImageService();
				 //String temp = tis.getString();
				   
				
				
				String temp = imageService.getServingUrl(bk);
				//put it in the list
				urls.add(temp);
			}else{
				break;
			}
		}
		return urls;
	}
	
	public List<String> getByAlbumName(String albumName){
		List<String> urls = new ArrayList<String>();
		ImagesService imageService = ImagesServiceFactory.getImagesService();
		//start objectify
		Objectify ofy = ObjectifyService.begin();
		//run a query on all photo where albumName
		Query<Photo> q = ofy.query(Photo.class).filter("albumName =", albumName);
		//from these photos get the blobkey then from that get the url add it to the list
		for(Photo p:q){
			/** 
			 * Testing code: swap 
			 *    'TestImageService tis = new TestImageService();'
			 *    'String tempURL = tis.getString();'
			 * 
			 * with 
			 * 
			 *    'String tempURL = imageService.getServingUrl(new BlobKey(p.getBlobKey()));' 
			 * to test
			 */
            //TestImageService tis = new TestImageService();
        	//String tempURL = tis.getString();
			 
			
			
            String tempURL = imageService.getServingUrl(new BlobKey(p.getBlobKey()));
			urls.add(tempURL);
		}
		return urls;
	}
	// this is identical to the getByAlbumName above, except that it also filters for a matching username
	public List<String> getByAlbumName(String albumName, String userName){
		List<String> urls = new ArrayList<String>();
		ImagesService imageService = ImagesServiceFactory.getImagesService();
		//start objectify
		Objectify ofy = ObjectifyService.begin();
		
		Query<Photo> q = ofy.query(Photo.class).filter("albumName =", albumName).filter("user =", userName);
		//from these photos get the blobkey then from that get the url add it to the list
		for(Photo p:q){
			
			/** 
			 * Testing code: swap 
			 *    'TestImageService tis = new TestImageService();'
			 *    'String tempURL = tis.getString();'
			 * 
			 * with 
			 * 
			 *    'String tempURL = imageService.getServingUrl(new BlobKey(p.getBlobKey()));' 
			 * to test
			 */

            //TestImageService tis = new TestImageService();
        	//String tempURL = tis.getString();			
			
            String tempURL = imageService.getServingUrl(new BlobKey(p.getBlobKey()));
			urls.add(tempURL);
		}
		return urls;
	}
	/*
	 * Returns a List of URLs for all of this user's photos with the given tag.
	 */
	public List<String> getByTagName(String tagName, String userName){
		List<String> urls = new ArrayList<String>();
		ImagesService imageService = ImagesServiceFactory.getImagesService();
		//start objectify
		Objectify ofy = ObjectifyService.begin();
		//run a query on all photo where user = userName
		Query<Photo> q = ofy.query(Photo.class).filter("user =", userName);
		//from these photos get the blobkey then from that get the url add it to the list
		for(Photo p:q){
			// filter for photos which include the requested tag
			ArrayList<String> tagList = (ArrayList<String>)p.getTagList();
			if (tagList.contains(tagName)) {
				String tempURL = imageService.getServingUrl(new BlobKey(p.getBlobKey()));
				urls.add(tempURL);
			}
		}
		return urls;
	}

	public void deleteAllPhotos(){
		Objectify ofy = ObjectifyService.begin();
		Iterable<Key<Photo>> allKeys = ofy.query(Photo.class).fetchKeys();
		ofy.delete(allKeys);
	}
	/**
	 * TestImageService is just puts out a string
	 * used to mock the getServingURL method
	 * 
	 * @author D
	 *
	 */
	public class TestImageService{
		String test;
		
		public TestImageService(){
			test = "TestString";
		}
		
		public String getString(){
			return test;
		}
	}	
	/* ***************************************
	 * 
	 */
	public List<com.google.appengine.api.datastore.Key> getKeysByTagName(String tagName, String userName){
		List<com.google.appengine.api.datastore.Key> keys = new ArrayList<com.google.appengine.api.datastore.Key>();
		//start objectify
		Objectify ofy = ObjectifyService.begin();
		//run a query on all photo where user = userName
		Query<Photo> q = ofy.query(Photo.class).filter("user =", userName);

		//from these photos get the blobkey then from that get the url add it to the list
		for(Photo p:q){
			// filter for photos which include the requested tag
			ArrayList<String> tagList = (ArrayList<String>)p.getTagList();
			if (tagList.contains(tagName)) {
				com.google.appengine.api.datastore.Key newKey = KeyFactory.createKey("Photo", p.getId());
				keys.add(newKey);
			}
		}
		return keys;
	}
	/* ***************************************** */

}
